package com.yu.rpc.core.fault.retry;

import com.github.rholder.retry.*;
import com.yu.rpc.core.model.RPCResponse;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * 固定间隔重试策略
 */
public class FixedIntervalRetryStrategy implements RetryStrategy {

    @Override
    public RPCResponse doRetry(Callable<RPCResponse> callable) throws Exception {
        Retryer<RPCResponse> retryer = RetryerBuilder.<RPCResponse>newBuilder()
                .retryIfException()
                .withWaitStrategy(WaitStrategies.fixedWait(3L, TimeUnit.SECONDS))
                .withStopStrategy(StopStrategies.stopAfterAttempt(3))
                .withRetryListener(new RetryListener() {
                    @Override
                    public <V> void onRetry(Attempt<V> attempt) {
                        System.out.println("重试次数：" + attempt.getAttemptNumber());
                    }
                })
                .build();
        return retryer.call(callable);
    }
}
